package com.asset.dispoal.dic.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.dic.domain.db.entity.GbDictDo;
import com.asset.dispoal.dic.domain.db.mapper.GbDictMapper;
import com.asset.dispoal.dic.dto.GbDicRequest;
import com.asset.dispoal.dic.service.converter.GbDicConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/2/24$
 */
@Component
public class GbDicQueryPageCommand implements BaseCommand<GbDicRequest, Result> {


    @Autowired
    GbDictMapper gbDictMapper;

    @Override
    public Result<PageVO> execute(GbDicRequest request) {
        GbDictDo gbDictDo = GbDicConverter.INSTANCE.reqToDo(request);
        gbDictDo.setPermission((String) GlobalHeaderThreadLocal.get().getExtraMap().get("permission"));
        Integer count = gbDictMapper.pageCount(gbDictDo);
        Page<GbDictDo> page = PagingQuery.getPage(count, gbDictDo);
        PageVO<GbDictDo> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            final List<GbDictDo> gbDictDos = gbDictMapper.queryPage(page);
            pageVO.setResult(gbDictDos);
        }
        return Result.ok(pageVO);
    }
}
